<?php
class UserAction extends CommonAction{

	public function showUsers(){
		$user = D("User");
		$userlist = $user->select();
		$this->assign('userlist',$userlist);
		$this -> display('User:showUsers');
	}

	/*
	 * 展示用户新增页面
	 */
	public function addUserPage(){
		$this->display();
	}

	/*
	 * 新增用户
	 */
	public function add(){
		$user = D("User");
		$requestData = $user->create();
		$username = $_POST['username'];
		$email = $_POST['email'];
		$phone = $_POST['phone'];
		$this->assign('username',$username);
		$this->assign('email',$email);
		$this->assign('phone',$phone);

		if ($requestData){
			$user->add();
			$this->showUsers();
		}else{
			$error = $user->getError();
			$this->assign('error',$error);
			$this->display('User:addUserPage');
		}

	}

	/*
	 * 编辑用户页面
	 */
	public function edit(){
		$user = D("User");
		$username = $_GET['username'];
		if (isset($username)){
			$userdata = $user->where("username='$username'")->find();
			$this->assign('userdata',$userdata);
		}
		$this->display('User:edit');
	}

	/*
	 * 更新用户页面
	 */
	public function update(){
		$user = D("User");
		$data = $user->create();
		unset($data['password']);//去掉password元素，不提交
		unset($data['reg_time']);//去掉reg_time元素，不提交
		$username = $_GET['username'];
		if ($data){
			$userdata = $user->where("username='$username'")->save($data);
			$this->showUsers();
		}else {
			$error = $user->getError();
			$this->assign('error',$error);
			$this->edit();
		}
	}
	
	/*
	 * 删除用户
	 */
	public function delete(){
		$user = D("User");
		$username = $_GET["username"];
		if (isset($username) && $username!='admin'){
			$user->where("username='$username'")->delete();
		}
		$this->showUsers();
	}

	/*
	 * 密码重置
	 */
	public function resetPsw(){
		$user = new UserModel();
		$username = $_GET["username"];
		if($user->resetPsw($username)){
			$msg = $username."的密码重置成功！";
		}else{
			$msg = $username."的密码重置失败！";
		}
		$this->assign('msg',$msg);
		$this->showUsers();
	}
}
?>